package org.esk.dablog.web.forms.forum;

import org.esk.dablog.web.forms.EditEntryForm;
import org.esk.dablog.model.forum.ForumTopicEntry;
import org.esk.dablog.model.forum.ForumPostEntry;
import org.esk.dablog.model.Entry;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.validation.BindException;
import org.acegisecurity.annotation.Secured;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.List;

/**
 * This class provides administrator's access to post editing 
 * User: esk
 * Date: 01.01.2007
 * Time: 21:16:31
 * $Id:$
 */
@Secured("ROLE_SUPERVISOR")
public class SystemEditPostForm extends EditEntryForm {


    protected void processEntry(Entry e) {
        ForumPostEntry entry = (ForumPostEntry) e;
        if(entry.getTopicId() != entry.getParent().getId()){
            Entry newParent = new Entry();
            newParent.setId(entry.getTopicId());
            entry.setParent(newParent);
        }
    }

    protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response, BindException errors, Map controlModel) throws Exception {
        ModelAndView result = super.showForm(request, response, errors, controlModel);

        List topics = getEntryManager().listEntries(ForumTopicEntry.class);
        result.addObject("forumTopics", topics);
        return result;
    }
}
